﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Diagnostics;

namespace WindowsPhoneAppHost
{
    public partial class FlowList : PhoneApplicationPage
    {
        public FlowList()
        {
            InitializeComponent();
            test();
//            DoHttpWebRequest();
        }

        private void test()
        {
            for (int i = 0; i < 5; i++)
            {
                FlowTitle item = new FlowTitle();
                listBox.Items.Add(item);
                listBox.Items.Add(new ListSeperator());
                for (int j = 0; j < 5 ; j++)
                {
                    FlowListBoxItem flowListBoxItem = new FlowListBoxItem();
                    flowListBoxItem.Subject = "费用报销流程";
                    listBox.Items.Add(flowListBoxItem);
                    listBox.Items.Add(new ListSeperator());
                }
            }
        }
        //*
        private void DoHttpWebRequest()
        {
            string url = "http://localhost/MobileOA/Flows.js";
            WebRequest request = WebRequest.Create(url); //创建WebRequest类
            IAsyncResult result = request.BeginGetResponse(ResponseCallback, request); //返回异步操作的状态
        }

        private void ResponseCallback(IAsyncResult result)
        {
            var request = (HttpWebRequest)result.AsyncState; //获取异步操作返回的的信息
            WebResponse response = request.EndGetResponse(result); //结束对 Internet 资源的异步请求
            using (Stream stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                string contents = reader.ReadToEnd();
                //Dispatcher.BeginInvoke(delegate() { this.textBlock1.Text = contents; });
                Dispatcher.BeginInvoke(delegate { ParseJson(contents); });
            }
        }

        private void ParseJson(string jsonString)
        {
            Debug.WriteLine(jsonString);
            JArray jArray = JArray.Parse(jsonString);
            Debug.WriteLine(jArray.Count.ToString());

            for (int i = 0; i < jArray.Count; i++)
            {
                var obj = (JObject)jArray[i];
                FlowTitle item = new FlowTitle();
                item.TitleText = Common.getJsonString(obj, "title");
                JArray list = (JArray)obj["list"];
                listBox.Items.Add(item);
                listBox.Items.Add(new ListSeperator());
                for (int j = 0; j < list.Count ; j++)
                {
                    var listobj = (JObject)list[i];
                    string str = Common.getJsonString(listobj, "title");
                    FlowListBoxItem flowListBoxItem = new FlowListBoxItem();
                    flowListBoxItem.Subject = str;
                    listBox.Items.Add(flowListBoxItem);
                    listBox.Items.Add(new ListSeperator());
                }
            }
        }
         //*/
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            //这里可以执行弹出对话框操作
            e.Cancel = true;  
            MessageBox.Show("返回键不再返回");
        }

    }
}